var defects = [];
var currentDefects = [];
var table;
var undefinedReplace = '空';
var start = $('#startTime').val();
var end = $('#endTime').val();
var stationId = $('#stationId').val();
var columns = [ '区域', '告警等级', '状态', '告警类型', '告警内容', '更新时间' ];
var initColumns = columns;
var stationsLength = 0;
function init() {
	$('#start').val($('#startTime').val());
	$('#end').val($('#endTime').val());
	$('#stationId').val($('#stationId').val());
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		todayHighlight : true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		pickerPosition : 'bottom-left',
		todayHighlight : true
	});
	loadData();
}
function loadData() {
	$.getJSON("/rest/getWeatherWarningTableDataByTime" + "/" + start + "/"
			+ end, function(result) {
			defects = result["data"];
			currentDefects = [];
			loadTable(defects);
	});
}
function query() {
	var s = $('#start').val();
	var e = $('#end').val();
	start = s;
	end = e;
	loadData();
}

function loadTable(defects) {
	if (table) {
		table.getTable().destroy();
	}
	var opt = MyTable.getDefaultOptions();
	opt.paging = false;
	opt.autoWidth = false;
	var defs = [ {
		"width" : "3%",
		"targets" : 0
	},// width为列宽，单位px或%均可；targets表示设置哪一列
	{
		"width" : "5%",
		"targets" : 1
	}, {
		"width" : "5%",
		"targets" : 2
	}, {
		"width" : "10%",
		"targets" : 3
	}, {
		"width" : "20%",
		"targets" : 4
	}, {
		"width" : "5%",
		"targets" : 5
	} ];
	opt.scrollY = scrollYHeight + "px";
	opt.scrollX = false;
	var datas = [];
	for ( var i in defects) {
		var f = defects[i];
		//'区域', '告警等级', '状态', '告警类型', '告警内容', '更新时间'
		datas.push([ str(f['name']), str(f['alevel']), str(f['astatus']),
				str(f['atype']), str(f['des']), time(f['pubDate'])]);
	}
	opt.columnDefs = defs;
	table = new MyTable("defectTable", columns, opt);
	table.setDataSet(datas).show();
	$('#dataEmpty').hide();
}
function minusTime(t1, t2) {
	var d1 = new Date(t1.time ? t1.time : t1);
	var d2 = new Date(t2.time ? t2.time : t2);
	return ((d1.getTime() - d2.getTime()) / 60000).toFixed(2);
}
function redrawTable() {
	if (table) {
		table.getTable().draw();
	}
}
function str(v) {
	return v ? v : '';
}
function time(v) {
	if (!v) {
		return '';
	} else {
		return new Date(v.time ? v.time : v).toLocaleString();
	}
}

function resizeHtml() {
	if (table) {
		table.getTable().draw();
	}
}